﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace DARE
{
    [AttrTypeDependentSerializable(typeof(CRadioButtonData))]
    public class CRadioButton : CButton
    {
        #region fields

        private bool m_isSelected;
        private CStaticImage m_radioCButtonIcon = null;

        #endregion

        #region properties



        /// <summary>
        /// Gets/Sets the selection state
        /// </summary>
        public bool IsSelected
        {
            get { return m_isSelected; }
            set
            {
                if (value != m_isSelected)
                {
                    if (value == true)
                    {
                        m_children["selected"].Show();
                        m_isSelected = true;
                    }
                    else
                    {
                        m_children["selected"].Hide();
                        m_isSelected = false;
                    }
                }
            }
        }

        /// <summary>
        /// Gets/Sets the radio button icon (the icon when this radio button is selected
        /// </summary>
        public CStaticImage CRadioButtonIcon
        { get { return m_radioCButtonIcon; } set { m_radioCButtonIcon = value; } }

        #endregion

        #region ctor

        public CRadioButton(CHUDSystem hud, CWidget parent, string name)
            : base(hud, parent, name)
        {
            m_isSelected = false;

            this.m_releasedTexture = HUD.Skinner.GetTexture("radiobuttonreleased");
            this.m_clickedTexture = HUD.Skinner.GetTexture("radiobuttonclicked");
            this.m_texture = this.m_releasedTexture;

            m_radioCButtonIcon = new CStaticImage(hud, this, name + "_selected");
            m_radioCButtonIcon.Image = HUD.Skinner.GetTexture("radiobuttonicon");
            m_radioCButtonIcon.Size = new Vector2(0.8f, 0.8f);
            m_radioCButtonIcon.Position = new Vector2(0.1f, 0.1f);
            m_radioCButtonIcon.Hide();
            Size = new Vector2(20, 20);
        }

        #endregion

        #region events

        /// <summary>
        /// Method called whenever the mouse is pressed and is over this widget
        /// </summary>
        public override bool onClick(CMouse mouse)
        {
            if (m_clickedTexture != null)
            {
                m_clickedTexture.Rectangle = m_texture.Rectangle;
                this.m_texture = m_clickedTexture;
            }
            InvokeClick(this);
            IsLeftClicked = true;
            return true;
        }

        /// <summary>
        /// Method called whenever the mouse is released and is over this widget and was pressed over this widget
        /// </summary>
        public override bool onRelease(CMouse mouse)
        {
            if (m_releasedTexture != null)
                this.m_texture = m_releasedTexture;
            if (IsLeftClicked && isMouseOver(mouse))
            {
                InvokeRelease(this);
                IsLeftClicked = false;
                return true;
            }
            return false;
        }

        #endregion
    }
}
